<?php
add_action('wp_head', 'comment');
function comment(){ ?>
<script>
    jQuery(document).ready(function($) {
        $('button[name="copycode"]').zclip({
            path: '<?php echo get_template_directory_uri().'/js/clipboard/ZeroClipboard.swf'?>',
            copy: $('input[name="coupon-code"]').val()
        });
        $('button[name="copycode"]').click(function(){
           $(this).text('Copied').addClass('copied'); 
        });
        var frm = $('#commentformpop');
        frm.submit(function(event){
            event.preventDefault();
            var comment_content = $('textarea[name="comment_content-pop"]').val();
            var comment_post_ID = $('input[name="comment_post_ID-pop"]').val();
            var comment_author_IP = $('input[name="comment_author_IP-pop"]').val();
            var comment_agent = $('input[name="comment_agent-pop"]').val();
            var comment_date = $('input[name="comment_date-pop"]').val();
            var comment_date_gmt = $('input[name="comment_date_gmt-pop"]').val();
            var user_id = $('input[name="user_id-pop"]').val();
            var comment_author = $('input[name="comment_author-pop"]').val();
            var comment_author_email = $('input[name="comment_author_email-pop"]').val();
            var user_url = $('input[name="user_url-pop"]').val();
            var security = $('input[name="6_letters_code_pop"]').val();
            var value_sending = [comment_content,comment_post_ID,comment_author_IP,comment_agent,comment_date,comment_date_gmt,user_id,comment_author,comment_author_email,user_url,security];
            var data ={
                    action : 'comment',
                    value_send : value_sending
                };
                jQuery.post(ajaxurl, data, function(result) {
                    var comment = $('ol.commentlist');
                    $('textarea[name="comment_content-pop"]').val('');
                    comment.empty().html(result);
                });
        });
    });
    function submit_cm(id){
            var comment_content = jQuery('textarea[name="comment_content'+id+'"]').val();
            if(comment_content == ''){
                jQuery('div.cm-missing').removeClass('hiden');
            }else{
                jQuery('div.cm-missing').addClass('hiden');
            }
            var comment_post_ID = jQuery('input[name="comment_post_ID'+id+'"]').val();
            var comment_author_IP = jQuery('input[name="comment_author_IP'+id+'"]').val();
            var comment_agent = jQuery('input[name="comment_agent'+id+'"]').val();
            var comment_date = jQuery('input[name="comment_date'+id+'"]').val();
            var comment_date_gmt = jQuery('input[name="comment_date_gmt'+id+'"]').val();
            var comment_author = jQuery('input[name="comment_author'+id+'"]').val();
            var user_id = jQuery('input[name="user_id'+id+'"]').val();
            var comment_author_email = jQuery('input[name="comment_author_email'+id+'"]').val();
            var user_url = jQuery('input[name="user_url'+id+'"]').val();
            var value_sending = [comment_content,comment_post_ID,comment_author_IP,comment_agent,comment_date,comment_date_gmt,user_id,comment_author,comment_author_email,user_url];
            var data ={
                    action : 'comment',
                    value_send : value_sending
                };
                jQuery.post(ajaxurl, data, function(result) {
                    jQuery('textarea[name="comment_content'+id+'"]').val('');
                    var comment = jQuery('ol#commentlist'+id);
                    comment.empty().html(result);
                });
    }
</script>
<?php }
add_action('wp_ajax_nopriv_comment', 'comment_callback');
add_action('wp_ajax_comment', 'comment_callback');
function comment_callback(){
    session_start();
    $value_received = $_POST['value_send'];
    $security = $value_received[10];
    $data = array(
        'comment_content' => $value_received[0],
        'comment_post_ID' => $value_received[1],
        'comment_author_IP' => $value_received[2],
        'comment_agent' => $value_received[3],
        'comment_date' => $value_received[4],
        'comment_date_gmt' => $value_received[5],
        'user_id' => $value_received[6],
        'comment_author' => $value_received[7],
        'comment_author_email' => $value_received[8],
        'comment_author_url' => $value_received[9]
    );
    if($value_received[0] != ''){
        wp_insert_comment($data);
    }
    $comments = get_comments(array(
                     'post_id' => $value_received[1],
                     'status' => 'approve'
               ));
               wp_list_comments(array(
                'per_page' => -1,
                'reverse_top_level' => false,
                'reply_text' => false,
                'callback' => 'bigsave_comment',
                   'short_ping'        => true
                ),
                $comments);
    die();
}
?>